package net.chenlin.dp;

import net.chenlin.dp.modules.mqtt.DeviceSubscribeSample;
import org.apache.tomcat.util.http.LegacyCookieProcessor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.Banner;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.embedded.tomcat.TomcatContextCustomizer;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableScheduling;

/**
 * 应用启动器
 * @author zcl<yczclcn@163.com>
 */
@SpringBootApplication
@ServletComponentScan
@EnableScheduling
public class DpApplication extends SpringBootServletInitializer implements CommandLineRunner {

    @Autowired
    private DeviceSubscribeSample sample;


    private static final Logger LOGGER = LoggerFactory.getLogger(DpApplication.class);

    /**
     * jar启动
     * @param args
     */
    public static void main(String[] args) {
        SpringApplication application = new SpringApplication(DpApplication.class);
        application.setBannerMode(Banner.Mode.OFF);
        application.run(args);

        //int port = 8090;
        //KeepServer server = new KeepServer(port);
        //server.start();
        //SubscribeSample sample = new SubscribeSample();
        //sample.start();

        LOGGER.info("The Dp application has been started successfully!");
        System.out.println("----------------------dp--service--ok------->------->-------------");
    }

    @Bean
    public WebServerFactoryCustomizer<TomcatServletWebServerFactory> cookieProcessorCustomizer() {
        return tomcatServletWebServerFactory -> tomcatServletWebServerFactory.addContextCustomizers((TomcatContextCustomizer) context -> {
            context.setCookieProcessor(new LegacyCookieProcessor());
        });
    }

    /**
     * war启动
     * @param builder
     * @return
     */
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        builder.bannerMode(Banner.Mode.OFF);
        SpringApplicationBuilder applicationBuilder = builder.sources(DpApplication.class);
        LOGGER.info("The Dp application has been started successfully!");
        return applicationBuilder;
    }

    @Override
    public void run(String... args) throws Exception {
        sample.startDevcieSubscribe();
    }
}
